package com.dubbo.microserviceweb.controller;

import com.dubbo.api.HotWordService;
import com.dubbo.api.PaperService;
import com.dubbo.api.model.BrowserModel;
import com.dubbo.api.model.HotWordModel;
import com.dubbo.api.model.KeywordModel;
import com.dubbo.api.model.PaperHWordModel;
import com.dubbo.microserviceweb.controller.viewobject.HotWordVO;
import com.dubbo.microserviceweb.controller.viewobject.PaperVO;
import com.dubbo.microserviceweb.response.CommonReturnType;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Key;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller("/hotword")
@RequestMapping("/hotword")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class HotWordController extends BaseController {

   @Reference
    private HotWordService hotWordService;


    @RequestMapping(value = "/browser",method = {RequestMethod.POST},consumes =CONTENT_TYPE_FORMED)
    @ResponseBody
    public CommonReturnType userReadPaper(@RequestParam("uId") Integer uId,
                                          @RequestParam("pId") Integer pId){

        BrowserModel browserModel = new BrowserModel();
        //绑定id
        browserModel.setpId(pId);
        browserModel.setuId(uId);
        hotWordService.userReadPaper(browserModel);
        //操作PW表，逻辑判断
        HotWordModel hotWordModel1 = new HotWordModel();
        List<String> pKword = hotWordService.getKWord(pId);
        for(int i = 0;i<pKword.size();i++) {
            hotWordModel1 = hotWordService.selectByHWord(pKword.get(i));
            if (hotWordModel1 == null) {
                //热词表空，执行插入PW和HW
                //取出关键词表中pid对应的关键词
                //插入热词表
                    //历史词频，取关键词表中关键词的相同数
                    HotWordModel hotWordModel = new HotWordModel();
                    KeywordModel keywordModel = new KeywordModel();
                    keywordModel = hotWordService.countKW(pKword.get(i));
                    hotWordModel.setHotHistory(keywordModel.getCount());
                    //当前词频+1
                    hotWordModel.setHotCurrent(keywordModel.getCount() + 1);
                    hotWordModel.setHotWord(pKword.get(i));
                    hotWordService.insertHW(hotWordModel);
                    //插入PW
                    PaperHWordModel paperHWordModel1 = new PaperHWordModel();
                    hotWordModel = hotWordService.selectByHWord(pKword.get(i));
                    paperHWordModel1.setHotId(hotWordModel.getHotId());
                    paperHWordModel1.setpId(pId);
                    hotWordService.insertPW(paperHWordModel1);
            } else {
                //更新热词表
                    HotWordModel hotWordModel = new HotWordModel();
                    hotWordModel.setHotWord(pKword.get(i));
                    hotWordService.updateCurrent(hotWordModel);
            }
        }

        return CommonReturnType.create(null);
    }


   @RequestMapping("/rank")
   @ResponseBody
   public CommonReturnType Rank() throws ParseException {
        List<HotWordModel> hotWordModelList = hotWordService.rankBefore();
        hotWordModelList = hotWordService.sort(hotWordModelList);
       List<HotWordVO> hotWordVOList =hotWordModelList.stream().map(hotWordModel -> {
           HotWordVO hotWordVO = this.convertVOFromModel(hotWordModel);
           return hotWordVO;
       }).collect(Collectors.toList());
       return CommonReturnType.create(hotWordVOList);
   }
    private HotWordVO convertVOFromModel(HotWordModel hotWordModel){
        if(hotWordModel== null){
            return null;
        }
        HotWordVO hotWordVO = new HotWordVO();
        BeanUtils.copyProperties(hotWordModel,hotWordVO);
        return hotWordVO;
    }
}
